package ch.fhnw.qualiscan.application.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.data.Container;
import com.vaadin.data.util.BeanItemContainer;

import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionnaireModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.views.DashboardView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.helpers.AdminQuestionnairesTableArgs;
import ch.fhnw.qualiscan.core.db.helpers.dashboardTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.Questionnaire;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;

@SuppressWarnings("serial")
public class Dashboard extends Controller {
	
	private List<SurveyUserRole> surList;
	
	private BeanItemContainer<dashboardTableArgs> tableContainer = new BeanItemContainer<dashboardTableArgs>(dashboardTableArgs.class);
	 
	private UserModel userModel;
	private SurveyModel surveyModel;
	private DomainModel domainModel;
	private QuestionnaireModel questionnaireModel;
	
	@Override
	public void init() {
		loadTableData();	
		
	}
	
	public Container getQuestionaireData() {
        final BeanItemContainer<AdminQuestionnairesTableArgs> tableContainer;
        tableContainer = new BeanItemContainer<AdminQuestionnairesTableArgs>(AdminQuestionnairesTableArgs.class);
        
        List<Questionnaire> questionnaires = questionnaireModel.getQuestionnaires(super.userSessionHelper.getUser());
        
        for(Questionnaire questionnaire : questionnaires) {
            AdminQuestionnairesTableArgs qta = new AdminQuestionnairesTableArgs(questionnaire, questionnaire.getTitle(), questionnaire.getDescription(), questionnaire.getCreated().toString());
            tableContainer.addBean(qta);
        }
        
        return tableContainer;
    }
	
	public void loadQuestionnairePage(Questionnaire questionnaire) {
        super.getAppData().getLoaderLib().setUrl("AdminQuestionnaire/0/" + questionnaire.getId());
    }
	
	public QuestionnaireModel getQuestionnaireModel() {
		return questionnaireModel;
	}
	
	private BeanItemContainer<dashboardTableArgs> loadTableData() {
		
		List<SurveyUserRole> surList = surveyModel.getSurListByUser(userSessionHelper.getUser());
		logger.info(""+surList.size()+" surveys were found in the database.");
		
		for(int i = 0; i < surList.size(); i++) {
			String surveyRole = userModel.getRoleText(surList.get(i).getRole(), userSession.getLanguage()).getTitle();
			String surveyStage = surveyModel.getStageText(surList.get(i).getSurvey().getStage(), userSession.getLanguage()).getTitle();
			
			dashboardTableArgs dta = new dashboardTableArgs(surList.get(i), surList.get(i).getSurvey().getTitle(), surveyRole, surList.get(i).getSurvey().getCompletiondate(), surveyStage, surList.get(i).getSurvey().getUniversity().getTitle());
			tableContainer.addBean(dta);
		}
		
		return tableContainer;
	}
	
public boolean surveyFilledOut(SurveyUserRole sur) {
		
		boolean isSurveyFinished = true;
		
		Iterator it = sur.getSurvey().getDomains().iterator();	
		while(it.hasNext()) {

			Domain d = (Domain)it.next();
			
			int nQuestions = 0;
			Iterator<Question> questionIterator= d.getQuestions().iterator();
			while (questionIterator.hasNext()) {
				Question q = questionIterator.next();
				if (q.getSubquestions().size() > 0) {
					nQuestions = nQuestions + q.getSubquestions().size();
				}
				else {
					nQuestions++;
				}
			}
			
			int totalAnswers = domainModel.getNumberOfAnswersForSurAndDomain(sur, d);
			float progress = (float)totalAnswers/(float)nQuestions;
			if (progress != 1.0) {
				isSurveyFinished = false;
			}
		}
		
		return isSurveyFinished;
	}

	@Override
	public void setNeededPermissions() {
		neededPermissions.add(Permissions.LOGGEDIN);	
	}


	@Override
	public void loadModels() {
		userModel = new UserModel();
		surveyModel = new SurveyModel();
		domainModel = new DomainModel();
		questionnaireModel = new QuestionnaireModel();
		
	}

	@Override
	public void setView() {
		view = new DashboardView(this);
		view.buildLayout();
	}

	public BeanItemContainer<dashboardTableArgs> getTableContainer() {
		return tableContainer;
	}

	@Override
	public void urlFragmentChanged(int fragmentId, String fragment) {
		// TODO Auto-generated method stub
		
	}




	
	


	

}
